﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SnippetDoc.Utils;
using SnippetDoc.Entities;
using SnippetDoc.Report;
using SnippetDoc.Properties;
using System.Diagnostics;

namespace SnippetDoc
{
    public partial class MainForm : Form
    {
        private FolderScanner scanner;

        public MainForm()
        {
            InitializeComponent();

            scanner = new FolderScanner();
            LoadSettings();

            this.Text = this.Text + " " +  Program.VERSION_NAME;
        }

        private void LoadSettings()
        {
            textBoxSourceFolder.Text = Settings.Default.lastSrcDir;
            textBoxDestination.Text = Settings.Default.lastDestDir;
        }

        private void SaveSettings()
        {
            Settings.Default.lastSrcDir = textBoxSourceFolder.Text;
            Settings.Default.lastDestDir = textBoxDestination.Text;
            Settings.Default.Save();
        }

        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            IEnumerable<SnippetFolder> snippetFolders =  scanner.ScanSnippetDirectory(textBoxSourceFolder.Text);
            
            HtmlReport report = new HtmlReport();
            report.CreateReport(textBoxDestination.Text, snippetFolders);

            Process.Start(textBoxDestination.Text + "\\snippetdoc\\snippetdoc.html");

            SaveSettings();
        }

        private void buttonSelectSource_Click(object sender, EventArgs e)
        {
            folderBrowserDialogSnippetSource.ShowDialog();
            textBoxSourceFolder.Text = folderBrowserDialogSnippetSource.SelectedPath;
        }

        private void buttonSelectDestination_Click(object sender, EventArgs e)
        {
            folderBrowserDialogSnippetSource.ShowDialog();
            textBoxDestination.Text = folderBrowserDialogSnippetSource.SelectedPath;
        }

        private void textBoxSourceFolder_Validating(object sender, CancelEventArgs e)
        {
            string error = null;
            if (string.IsNullOrEmpty(textBoxSourceFolder.Text))
            {
                error = "Plz specify the src folder where your snippets are.";
                e.Cancel = true;
            }

            errorProvider1.SetError((Control)sender, error);
        }

        private void textBoxDestination_Validating(object sender, CancelEventArgs e)
        {
            string error = null;
            if (string.IsNullOrEmpty(textBoxDestination.Text))
            {
                error = "Plz specify the destination folder where the doc should be generated.";
                e.Cancel = true;
            }

            errorProvider1.SetError((Control)sender, error);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            e.Cancel = false;
        }
    }
}
